﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage:System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Request.Cookies["loginname"] == null || Request.Cookies["loginname"].Value=="")
        {
            Response.Redirect("~/login.aspx");
        }

        if (System.Configuration.ConfigurationManager.AppSettings["PowerId"] != ""&&System.Configuration.ConfigurationManager.AppSettings["PowerId"]!=null)
        {
            string powerId = System.Configuration.ConfigurationManager.AppSettings["PowerId"];

            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();

            if (Request.Cookies["loginname"] != null)
            {
                string loginname = Server.HtmlEncode(Request.Cookies["loginname"].Value);
                int roleid = dal.User.First(x => x.LoginName.Contains(loginname)).Role.RoleId;
                var list = dal.Role_Power.Where(x => x.RoleId == roleid);
                if (list.Count() > 0)
                {
                    bool result = false;
                    foreach (var x in list)
                    {
                        if (x.Pow_Id == powerId)
                        {
                           result=true;
                            break;
                        }
                    }
                    if (result == false)
                    {
                        Response.Redirect("~/error.aspx");
                    }
                }
                else {
                    Response.Redirect("~/error.aspx?msg='您没有权限查看'");
                 
                }
            }
            else {
                Response.Redirect("~/login.aspx");
            }
        }
    }
}